﻿#include "datathread.h"

DataThread::DataThread()
{
	qRegisterMetaType <QVector<int>>("QVector<int>");
    qDebug() << "trajdate->" << QThread::currentThreadId();
}

void DataThread::run(){
    analyserDate();
}

void DataThread::analyserDate(){
    int num = 0;
    QByteArray ary[8192];
    QTextStream textStream(&fileary[0], QIODevice::ReadOnly);
    while (!textStream.atEnd()) {
        QString line = textStream.readLine();
        int index = line.lastIndexOf(":");
        if(index > 0){
            line.remove(0,index + 1);
            line = line.remove(' ');
            //qDebug() << line;
            ary[num].clear();
            ary[num] = hexStrToByteArray(line);
            //qDebug() << ary[num].toHex();
            num++;

        }
        if(num >= 8000){
            num = 0;
        }
    }
    QXlsx::Document xlsx(QDir::currentPath() + "/" + QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss") + "data.xlsx");
    QXlsx::CellRange range = xlsx.dimension();
    int rowCounts = 0;
    xlsx.write(rowCounts + 1, 1, "cross_id");
    xlsx.write(rowCounts + 1, 2, "module_name");
    xlsx.write(rowCounts + 1, 3, "sequence_num");
    xlsx.write(rowCounts + 1, 4, "timestamp_sec");
    xlsx.write(rowCounts + 1, 5, "traffic_flow");
    xlsx.write(rowCounts + 1, 6, "traffic_number");
    xlsx.write(rowCounts + 1, 7, "head_time_diff");
    xlsx.write(rowCounts + 1, 8, "lane_id");
    xlsx.write(rowCounts + 1, 9, "branch_id");
    xlsx.write(rowCounts + 1, 10, "over_flow_occur");
    xlsx.write(rowCounts + 1, 11, "over_flow_forecast");
    xlsx.write(rowCounts + 1, 12, "over_flow_grid_lock");
    xlsx.write(rowCounts + 1, 13, "space_type");
    xlsx.write(rowCounts + 1, 14, "car_count");
    xlsx.write(rowCounts + 1, 15, "car_trans_count");
    xlsx.write(rowCounts + 1, 16, "queue_count");
    xlsx.write(rowCounts + 1, 17, "queue_trans_count");
    xlsx.write(rowCounts + 1, 18, "ped_num");
    xlsx.write(rowCounts + 1, 19, "bicycle_num");
    xlsx.write(rowCounts + 1, 20, "avg_speed");
    xlsx.write(rowCounts + 1, 21, "lane_occupation_ratio");
    xlsx.write(rowCounts + 1, 22, "queue_length");
    xlsx.save();
    unsigned char array[8192];
    TrafficInfos message;
    for(int i = 0;i<num;i++){
        memset(array,0,sizeof (array));
        //反序列化
        memset(&message,0,sizeof (TrafficInfos));
        memcpy(array,ary[i].data(),ary[i].size());
        pb_istream_t stream = pb_istream_from_buffer(array, ary[i].size());

        bool status = pb_decode(&stream, TrafficInfos_fields, &message);
        if(status){
            //qDebug() << message.traffic_info_count;
            for(int j = 0;j < message.traffic_info_count;j++){
                QXlsx::CellRange range = xlsx.dimension();
                rowCounts = range.lastRow();
                if(message.traffic_info[j].has_header){
                    //qDebug() << QString::number(j) + "->" << message.traffic_info[j].header.cross_id;
                    xlsx.write(rowCounts + 1, 1, message.traffic_info[j].header.cross_id);
                    //qDebug() << QString::number(j) + "->" << message.traffic_info[j].header.module_name;
                    xlsx.write(rowCounts + 1, 2, message.traffic_info[j].header.module_name);
                   // qDebug() << QString::number(j) + "->" << message.traffic_info[j].header.sequence_num;
                    xlsx.write(rowCounts + 1, 3, message.traffic_info[j].header.sequence_num);
                   // qDebug() << QString::number(j) + "->" << message.traffic_info[j].header.timestamp_sec;
                    // 时间戳转换为日期时间格式
                    QDateTime dateTime = QDateTime::fromMSecsSinceEpoch(message.traffic_info[j].header.timestamp_sec);

                    // 时间转换为字符串（年月日时分秒毫秒）
                    QString strBuffer = dateTime.toString("yyyy-MM-dd hh:mm:ss");
                    xlsx.write(rowCounts + 1, 4, strBuffer);
                }
                if(message.traffic_info[j].has_traffic_flow){
                   // qDebug() << QString::number(j) + "->" << message.traffic_info[j].traffic_flow;
                    xlsx.write(rowCounts + 1, 5, message.traffic_info[j].traffic_flow);
                }
                if(message.traffic_info[j].has_traffic_number){
                   // qDebug() << QString::number(j) + "->" << message.traffic_info[j].traffic_number;
                    xlsx.write(rowCounts + 1, 6, message.traffic_info[j].traffic_number);
                }
                if(message.traffic_info[j].has_head_time_diff){
                  //  qDebug() << QString::number(j) + "->" << message.traffic_info[j].head_time_diff;
                    xlsx.write(rowCounts + 1, 7, message.traffic_info[j].head_time_diff);
                }
                if(message.traffic_info[j].has_lane_id){
                  //  qDebug() << QString::number(j) + "->" << message.traffic_info[j].lane_id;
                    xlsx.write(rowCounts + 1, 8, message.traffic_info[j].lane_id);
                }
                if(message.traffic_info[j].has_branch_id){
                 //   qDebug() << QString::number(j) + "->" << message.traffic_info[j].branch_id;
                    xlsx.write(rowCounts + 1, 9, message.traffic_info[j].branch_id);
                }
                if(message.traffic_info[j].has_over_flow_occur){
                 //   qDebug() << QString::number(j) + "->" << message.traffic_info[j].over_flow_occur;
                    xlsx.write(rowCounts + 1, 10, message.traffic_info[j].over_flow_occur);
                }
                if(message.traffic_info[j].has_over_flow_forecast){
                  //  qDebug() << QString::number(j) + "->" << message.traffic_info[j].over_flow_forecast;
                    xlsx.write(rowCounts + 1, 11, message.traffic_info[j].over_flow_forecast);
                }
                if(message.traffic_info[j].has_over_flow_grid_lock){
                  //  qDebug() << QString::number(j) + "->" << message.traffic_info[j].over_flow_grid_lock;
                    xlsx.write(rowCounts + 1, 12, message.traffic_info[j].over_flow_grid_lock);
                }
                if(message.traffic_info[j].has_space_type){
                  //  qDebug() << QString::number(j) + "->" << message.traffic_info[j].space_type;
                    xlsx.write(rowCounts + 1, 13, message.traffic_info[j].space_type);
                }
                if(message.traffic_info[j].has_car_count){
                  //  qDebug() << QString::number(j) + "->" << message.traffic_info[j].car_count;
                    xlsx.write(rowCounts + 1, 14, message.traffic_info[j].car_count);
                }
                if(message.traffic_info[j].has_car_trans_count){
                  //  qDebug() << QString::number(j) + "->" << message.traffic_info[j].car_trans_count;
                    xlsx.write(rowCounts + 1, 15, message.traffic_info[j].car_trans_count);
                }
                if(message.traffic_info[j].has_queue_count){
                 //   qDebug() << QString::number(j) + "->" << message.traffic_info[j].queue_count;
                    xlsx.write(rowCounts + 1, 16, message.traffic_info[j].queue_count);
                }
                if(message.traffic_info[j].has_queue_trans_count){
                 //   qDebug() << QString::number(j) + "->" << message.traffic_info[j].queue_trans_count;
                    xlsx.write(rowCounts + 1, 17, message.traffic_info[j].queue_trans_count);
                }
                if(message.traffic_info[j].has_ped_num){
                 //   qDebug() << QString::number(j) + "->" << message.traffic_info[j].ped_num;
                    xlsx.write(rowCounts + 1, 18, message.traffic_info[j].ped_num);
                }
                if(message.traffic_info[j].has_bicycle_num){
                 //   qDebug() << QString::number(j) + "->" << message.traffic_info[j].bicycle_num;
                    xlsx.write(rowCounts + 1, 19, message.traffic_info[j].bicycle_num);
                }
                if(message.traffic_info[j].has_avg_speed){
                 //   qDebug() << QString::number(j) + "->" << message.traffic_info[j].avg_speed;
                    xlsx.write(rowCounts + 1, 20, message.traffic_info[j].avg_speed);
                }
                if(message.traffic_info[j].has_lane_occupation_ratio){
                 //   qDebug() << QString::number(j) + "->" << message.traffic_info[j].lane_occupation_ratio;
                    xlsx.write(rowCounts + 1, 21, message.traffic_info[j].lane_occupation_ratio);
                }
                if(message.traffic_info[j].has_queue_length){
                 //   qDebug() << QString::number(j) + "->" << message.traffic_info[j].lane_occupation_ratio;
                    xlsx.write(rowCounts + 1, 22, message.traffic_info[j].queue_length);
                }
            }
        }
               emit sendprogress((float)i / num);
    }
    xlsx.save();
    emit sendsize(0);
}


QByteArray DataThread::hexStrToByteArray(const QString &str)
{
    QByteArray senddata;
    int hexdata, lowhexdata;
    int hexdatalen = 0;
    int len = str.length();
    senddata.resize(len / 2);
    char lstr, hstr;

    for (int i = 0; i < len;) {
        hstr = str.at(i).toLatin1();
        if (hstr == ' ') {
            i++;
            continue;
        }

        i++;
        if (i >= len) {
            break;
        }

        lstr = str.at(i).toLatin1();
        hexdata = convertHexChar(hstr);
        lowhexdata = convertHexChar(lstr);

        if ((hexdata == 16) || (lowhexdata == 16)) {
            break;
        }
        else {
            hexdata = hexdata * 16 + lowhexdata;
        }

        i++;
        senddata[hexdatalen] = (char)hexdata;
        hexdatalen++;
    }

    senddata.resize(hexdatalen);
    return senddata;
}

char DataThread::convertHexChar(char ch)
{
    if ((ch >= '0') && (ch <= '9')) {
        return ch - 0x30;
    }
    else if ((ch >= 'A') && (ch <= 'F')) {
        return ch - 'A' + 10;
    }
    else if ((ch >= 'a') && (ch <= 'f')) {
        return ch - 'a' + 10;
    }
    else {
        return (-1);
    }
}
